﻿/*
创建人：longb
日期：2022-06-14
描述：试车通知单js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    pageType: getParam("type"), //0：创建 1：审核
    rootPath: getRootPath(), //网站根路径
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSTestRunNoticeBillHandler.ashx",
    comboxUrl:getRootPath() + "/Ashx/ZsHandler/ZSTestRunNoticeBillHandler.ashx",//combox下拉-调用后台的URL
    comboxAction:"GetMfgorder",//combox下拉-调用后台的具体方法
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
        $("#EditorpanelSection").hide();
        if (page.pageType == "0") {
            page.InitData();
            $("#SaveCreate").css("display", "");
            $("#EditorpanelSection").show();
        }
        if (page.pageType == "1") {
            $("#ConfirmRetrun").css("display", "");
        }
        if (page.pageType == "2") {
            $("#Recieve").css("display", "");
        }

    },
    //页面事件绑定
    bind: function () {
        var Initid = 0;
        var data = [{ id: 0, text: '待提交' }, { id: 1, text: '待审核' },  { id: 5, text: '待接收' }, { id: 10, text: '已接收'}];
        if (page.pageType == "1") {
            data = [{ id: 1, text: '待审核' },  { id: 5, text: '待接收' }, { id: 10, text: '已接收'}];
            Initid = 1;
        }
        if (page.pageType == "2") {
            data = [{ id: 5, text: '待接收' }, { id: 10, text: '已接收'}];
            Initid = 5;
        }
        $('#Status').combobox({
            data: data,
            valueField: 'id',
            textField: 'text',
            onSelect: function (record) {
                if (record.id != 0) {
                    $('#DeleteNoticeBillData').linkbutton("disable"); //禁止删除按钮
                    $('#SubmitNoticeBillData').linkbutton("disable");
                } else {
                    $('#DeleteNoticeBillData').linkbutton("enable").attr("onclick", "page.DeleteNoticeBillData()");
                    $('#SubmitNoticeBillData').linkbutton("enable").attr("onclick", "page.SubmitNoticeBillData()");
                }
                if (record.id != 1) {
                    $('#ConfirmNoticeBillData').linkbutton("disable"); //禁止配送按钮
                    $('#RetrunNoticeBillData').linkbutton("disable")
                } else {
                    $('#ConfirmNoticeBillData').linkbutton("enable").attr("onclick", "page.ConfirmNoticeBillData()");
                    $('#RetrunNoticeBillData').linkbutton("enable").attr("onclick", "page.RetrunNoticeBillData()");
                }
                if (record.id != 5) {
                    $('#RecieveNoticeBillData').linkbutton("disable")
                } else {
                    $('#RecieveNoticeBillData').linkbutton("enable").attr("onclick", "page.RecieveNoticeBillData()");
                }

            }
        });
        $('#TestRunType').combobox({
            data: [{ id: "0", text: '工厂试车' }, { id: "1", text: '检验试车' }, { id: "2", text: '附加试车'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#Status').combobox('setValue', Initid);
//        $('#TestRunType').combobox('setValue', 0);


        $('#mfgCombox').selectCombox({
            search: function () { },
            cancel: function () {$('#MFGORDERID').val(''); $('#ATTRIBUTEVALUE').textbox('setValue','');},
            selected: function (val, text,record){
                $('#MFGORDERID').val(val);
                $('#txtCombox').textbox('setValue',text);
                $('#ATTRIBUTEVALUE').textbox('setValue', record.ATTRIBUTEVALUE);
                $('#divComboxTxt').show();
                $('#divComboxSelect').hide();
                $('#divComboxSearch').show();
                $('#divComboxClear').hide();
            }
        });
        //任务单信息
        $("#NoticeBillTable").datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            rownumbers: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ZSTESTRUNNOTICEBILLID', width: 120, checkbox: true },
                 { field: 'ZSTESTRUNNOTICEBILLNO', title: '试车通知单号', width: 120 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 120 },
		         { field: 'PRODUCTFAMILYNAME', title: '型号', width: 130 }, //--
                 {field: 'TEAMEMPLOYEE', title: '生产工（组）长', width: 120 },
                 { field: 'ASSEMBLYDATE', title: '装配完工日期', width: 90 },
                 { field: 'TESTRUNTYPENAME', title: '试车类型', width: 120,  },
                 { field: 'WORKCENTERNAME', title: '责任单位', width: 100 },
                 { field: 'RESOURCENAME', title: '试车台', width: 100 },
                 { field: 'STATUS', title: '试车通知单状态', width: 100,  },
                 { field: 'NOTES', title: '备注说明', width: 100 },
                 { field: 'BILLTYPE', title: '来源', width: 100,  },
                 { field: 'CREATEEMPLOYEE', title: '创建人', width: 100, },
                 { field: 'CREATEDATE', title: '创建时间', width: 100 },
                 { field: 'AUDITEMPLOYEE', title: '审核人', width: 100,},
                 { field: 'AUDITDATE', title: '审核时间', width: 100 },
                 { field: 'RECIEVEEEMPLOYEE', title: '接收人', width: 100,  },
                 { field: 'RECIEVEDATE', title: '接收时间', width: 100 }
	           ]],
            onSelect: function (index, row) {
                row["MFGORDERNO"] = row.MFGORDERID;
                $('#EditorForm').form('load', row);
                if(!$('#btnComboxSearch').hasClass('l-btn-disabled')){//防止连续行点击，查询按钮事件失效
                    $('#txtCombox').combobox('disable');
                    $('#btnComboxSearch').linkbutton('disable');
                }
                $("#NoticeBillTable").datagrid("uncheckAll").datagrid("checkRow",index);//单选行，去掉同时勾选
                
            },
            onCheckAll:function(rows){
                $("#NoticeBillTable").datagrid("clearSelections");
                 $('#EditorForm').form("clear");
            },
            onCheck:function(rowIndex,rowData){
                var checkRows= $("#NoticeBillTable").datagrid("getChecked");
                var selectRow= $("#NoticeBillTable").datagrid("getSelected");
                if(checkRows.length>1){
                    $("#NoticeBillTable").datagrid("clearSelections");
                     $('#EditorForm').form("clear");
                }
                //if(selectRow==null&&checkRows.length==1){
                //     $("#NoticeBillTable").datagrid("clearSelections").datagrid("selectRow",rowIndex);
                //     $('#EditorForm').form('load', checkRows[0]);
                //}
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetNoticeBillData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetNoticeBillData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });
    },
    //初始化下拉选择数据
    InitData: function () {
//        page.post({ action: "GetMfgorder" }, function (res) {
//            $('#MFGORDERNO').combobox({
//                data: res.DBTable,
//                valueField: 'MFGORDERID',
//                textField: 'MFGORDERNAME',
//                onSelect: function (record) {
//                    $('#ATTRIBUTEVALUE').textbox('setValue', record.ATTRIBUTEVALUE);
//                    $('#MFGORDERID').val(record.MFGORDERID);
//                }
//            });
//        });
        page.post({ action: "GetDutyWorkcenter" }, function (res) {
            $('#DutyWorkcenterID').combobox({
                data: res.DBTable,
                valueField: 'WORKCENTERID',
                textField: 'WORKCENTERNAME'
            });
        });
        page.post({ action: "GetResourceDef" }, function (res) {
            $('#TestBed').combobox({
                data: res.DBTable,
                valueField: 'RESOURCEID',
                textField: 'RESOURCENAME'
            });
        });
        page.post({ action: "GetTeamEmployees" }, function (res) {
            $('#AssemblyWorkerID').combobox({
                data: res.DBTable,
                valueField: 'EMPLOYEEID',
                textField: 'FULLNAME'
            });
        });
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetNoticeBillData();
        page.CreateNoticeBillData();
    },
    //查询试车通知单信息
    GetNoticeBillData: function () {
        var frdata = $('#searchForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetNoticeBillData",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post(param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                $('#NoticeBillTable').datagrid('loadData', res.DBTable);
            }
        });
    },
    //保存试车通知单信息
    SaveNoticeBillData: function () {
        var row = $('#NoticeBillTable').datagrid('getSelected');
        if (row != null && row.STATUS != "待提交") { displayMessage(row.STATUS + "的通知单，不能保存", false); return; }
        var frdata = $('#EditorForm').serializeArray();
        var data = {};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "SaveNoticeBillData",
            data: JSON.stringify([data])
        };
        if (data.MFGORDERID == "") { displayMessage("订单号不能为空，请选择", false); return; }
        if (data.ASSEMBLYWORKERID == "") { displayMessage("生成工(组)长不能为空，请选择", false); return; }
        if (data.ASSEMBLYDATE == "") { displayMessage("装配完工日期不能为空，请选择", false); return; }
        if (data.TESTRUNTYPE == "") { displayMessage("试车类型不能为空，请选择", false); return; }
        if (data.DUTYWORKCENTERID == "") { displayMessage("责任单位不能为空，请选择", false); return; }
        if (data.TESTBED == "") { displayMessage("试车台不能为空，请选择", false); return; }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetNoticeBillData();
                page.CreateNoticeBillData();
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //新建试车通知单信息
    CreateNoticeBillData: function () {
        $('#EditorForm').form("clear");
        $('#txtCombox').combobox('enable');
        $('#btnComboxSearch').linkbutton('enable');
    },
    //提交审核试车通知单信息
    SubmitNoticeBillData: function () {
        page.NoticeBillData("SubmitNoticeBillData");
    },
    //删除试车通知单信息
    DeleteNoticeBillData: function () {
        page.NoticeBillData("DeleteNoticeBillData");
    },
    //审核试车通知单信息
    ConfirmNoticeBillData: function () {
        page.NoticeBillData("ConfirmNoticeBillData");
    },
    //退回试车通知单信息
    RetrunNoticeBillData: function () {
        page.NoticeBillData("RetrunNoticeBillData");
    },
    //接收试车通知单信息
    RecieveNoticeBillData: function () {
        page.NoticeBillData("RecieveNoticeBillData");
    },
    NoticeBillData: function (action) {
        var ids = [];
        var rows = $('#NoticeBillTable').datagrid("getChecked");
        $.each(rows, function (index, item) {
            ids.push(item.ZSTESTRUNNOTICEBILLID);
        });
        var param = {
            action: action,
            ids: ids.join(',')
        };
        if (ids.length == 0) { displayMessage("请勾选记录再进行操作", false); return; }
        page.post(param, function (res) {
            if (res.Result) {
                page.GetNoticeBillData();
                if(action=="DeleteNoticeBillData"){page.CreateNoticeBillData();}
            }
            displayMessage(res.Message, res.Result);
        });
    },
    //打印试车通知单信息
    PrintNoticeBillData: function () {

    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },

    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (param, callback) {
        $.ajax({
            type: "Post",
            url: page.ajaxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});